package com.minzheng.blog.controller;

import cn.hutool.json.JSONObject;
import com.minzheng.blog.annotation.OptLog;
import com.minzheng.blog.dto.MovieDTO;
import com.minzheng.blog.entity.Movie;
import com.minzheng.blog.service.MovieService;
import com.minzheng.blog.vo.ConditionVO;
import com.minzheng.blog.vo.MovieVO;
import com.minzheng.blog.vo.PageResult;
import com.minzheng.blog.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

import static com.minzheng.blog.constant.OptTypeConst.REMOVE;
import static com.minzheng.blog.constant.OptTypeConst.SAVE_OR_UPDATE;

/**
 * 电影控制器
 *
 * @author silencezheng
 * @date 2022/09/04
 */
@Api(tags = "电影模块")
@RestController
public class MovieController {
    @Autowired
    private MovieService movieService;

    /**
     * 查看电影列表
     *
     * @param condition 条件
     * @return {@link Result<MovieDTO>} 电影列表
     */
    @ApiOperation(value = "查看电影列表")
    @GetMapping("/movies/list")
    public Result<PageResult<MovieDTO>> listMovies(ConditionVO condition) {
        return Result.ok(movieService.listMovies(condition));
    }
    /**
     * 查看电影详情
     *
     * @param id 条件
     * @return {@link Result<Movie>} 电影详情
     */
    @ApiOperation(value = "查看电影详情")
    @GetMapping("/movies/listDetails")
    public Result<Movie> listMovieById(Integer id) {
        return Result.ok(movieService.listMovieById(id));
    }
    /**
     * 为电影评分
     * @return {@link Result<>}
     */
    @ApiOperation(value = "为电影评分")
    @PostMapping("/movies/toRate")
    public Result<?> toRate(@RequestBody JSONObject json) {
        System.out.println(json);
        movieService.toRate(Integer.parseInt(json.get("id").toString()),Integer.parseInt(json.get("userRate").toString()));
        return Result.ok();
    }


    /**
     * 查看后台电影列表
     *
     * @param condition 条件
     * @return {@link Result<MovieDTO>} 电影列表
     */
    @ApiOperation(value = "查看后台电影列表")
    @GetMapping("/admin/movies/list")
    public Result<PageResult<MovieDTO>> listBackMovies(ConditionVO condition) {
        return Result.ok(movieService.listMovies(condition));
    }

    /**
     * 添加或修改电影
     *
     * @param movieVO 电影信息
     * @return {@link Result<>}
     */
    @OptLog(optType = SAVE_OR_UPDATE)
    @ApiOperation(value = "添加或修改电影")
    @PostMapping("/admin/movie")
    public Result<?> saveOrUpdateMovie(@Valid @RequestBody MovieVO movieVO) {
        movieService.saveOrUpdateMovie(movieVO);
        return Result.ok();
    }

    /**
     * 删除电影
     *
     * @param movieIdList 电影id列表
     * @return {@link Result<>}
     */
    @OptLog(optType = REMOVE)
    @ApiOperation(value = "删除电影")
    @DeleteMapping("/admin/movies")
    public Result<?> deleteMovies(@RequestBody List<Integer> movieIdList) {
        movieService.deleteMovies(movieIdList);
        return Result.ok();
    }
}
